package daosimpl;

import java.util.ArrayList;
import java.util.List;

import model.Curso;
import model.PreguntaEstado;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Transaction;

import util.HibernateUtil;
import daos.PreguntaEstadoDAO;

public class PreguntaEstadoDAOImplH implements PreguntaEstadoDAO {
	private Transaction tx;

	@Override
	public List<PreguntaEstado> ListarEstados() throws HibernateException {
		List<PreguntaEstado> result = new ArrayList<PreguntaEstado>();
		try {			
			tx = HibernateUtil.session.beginTransaction();
						
			Query query = HibernateUtil.session
					.createQuery("FROM PreguntaEstado");
			result = query.list();
			System.out.println(query.list());
			System.out.println(query.list().size());
			if (query.list().isEmpty()) {
				System.out.println("esta vacio");
			}
			tx.commit();
			if (!tx.wasCommitted()){
			    tx.commit();
			}			
			return result;
			
		} catch (Exception he) {
			System.out.println("mall " + he.getMessage() + he.getCause());
			tx.rollback(); 
		} finally { 
            //session.close(); 
        }  
		return result; 
	}

	@Override
	public PreguntaEstado FindById(String id) throws HibernateException {
		PreguntaEstado result = new PreguntaEstado();
		try {			
			tx = HibernateUtil.session.beginTransaction();
						
			Query query = HibernateUtil.session
					.createQuery("FROM PreguntaEstado where estadoId='"+id+"'");
			result = (PreguntaEstado) query.list().get(0);
			System.out.println(query.list());
			System.out.println(query.list().size());
			if (query.list().isEmpty()) {
				System.out.println("esta vacio");
			}
			tx.commit();
			if (!tx.wasCommitted()){
			    tx.commit();
			}			
			return result;
			
		} catch (Exception he) {
			System.out.println("mall " + he.getMessage() + he.getCause());
			tx.rollback(); 
		} finally { 
            //session.close(); 
        }  
		return result; 
	}

}
